﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;

namespace Bookkeeping.Core.Services
{
    public class DateTimeHelper
    {
        public static DateTime GetPreviousMonth(DateTime date)
        {
            int previousMonth = date.Month - 1;
            int yearOfPreviousMonth = date.Year;

            if (previousMonth < 1)
            {
                previousMonth = 12;
                yearOfPreviousMonth--;
            }

            return new DateTime(yearOfPreviousMonth, previousMonth, date.Day);
        }

        public static DateTime GetNextMonth(DateTime date)
        {
            int nextMonth = date.Month + 1;
            int yearOfNextMonth = date.Year;

            if (nextMonth > 12)
            {
                nextMonth = 1;
                yearOfNextMonth++;
            }

            return new DateTime(yearOfNextMonth, nextMonth, date.Day);
        }

        public static bool IsSameMonthAndYear(DateTime date1, DateTime date2)
        {
            return date1.Month == date2.Month && date1.Year == date2.Year;
        }

        public static DateTime GetFirstDateTimeInMonth(DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1);
        }

        public static DateTime GetLastDateTimeInMonth(DateTime date)
        {
            int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);

            return new DateTime(date.Year, date.Month, daysInMonth, 23, 59, 59);
        }

        public static string GetMonthName(DateTime date, CultureInfo culture)
        {
            return culture.DateTimeFormat.GetMonthName(date.Month);
        }

        public static string GetMonthName(DateTime date)
        {
            return GetMonthName(date, Thread.CurrentThread.CurrentCulture);
        }
    }
}
